import { ref, computed } from 'vue'
import { defineStore } from 'pinia'

interface IField {
  column: any[],
  filterColumn: any[],
  originColumn: any[],
  keyword: string
}

export const useGroupStore = defineStore('group', () => {
  const group = ref<IField[]>([])

  // 设置当前字段的方法
  function setGroup(val: any[]) {
    group.value = val.map(item => {
      const list = item.column;
      return {
        ...item,
        filterColumn: [...list],
        originColumn: [...list],
        keyword: ''
      }

    })
  }

  function setKeyword(index: number, val: string) {
    group.value[index].keyword = val;
    if (val) {
      group.value[index].filterColumn = group.value[index].originColumn.filter(i => i.label.includes(val))
    }
    else {
      group.value[index].filterColumn = group.value[index].originColumn
    }
  }

  function deleteField(index: number, val: any) {
    group.value[index].column.splice(group.value[index].column.indexOf(val), 1)
  }

  return { group, setGroup, setKeyword, deleteField }
})
